package com.xty.xtyle.db.cloth;

import android.text.TextUtils;

import com.xty.xtyle.db.User;

import org.litepal.LitePal;

import java.util.List;

public class ClothDao {

    public void add(Cloth cloth) {
        if (null != cloth) cloth.save();
    }

    public void update(Cloth cloth, String newName) {
        if (null == cloth || TextUtils.isEmpty(newName)) return;
        cloth.updateAll("name = ?",newName);
    }

    public void update(Cloth cloth, int id) {
        if (id < 0) return;
        cloth.updateAll("id = ?",String.valueOf(id));
    }

    public void delete(String name) {
        if (TextUtils.isEmpty(name)) return;
        LitePal.deleteAll(Cloth.class, "name=?", name);
    }

    public List<Cloth> findAll() {
        return LitePal.order("price desc").find(Cloth.class);
    }

    public List<Cloth> findAllByTypes(String type) {
        try{
            return LitePal.where("type=?", type).find(Cloth.class);
        }catch (Exception e) {
            e.printStackTrace();
            return null;
        }

    }

    public Cloth findCloth(int id) {
        if (id < 0) return null;
        return LitePal.where("id=?", String.valueOf(id)).findFirst(Cloth.class);
    }
    public void delete(int id) {
        if (id < 0) return;
        LitePal.deleteAll(Cloth.class, "id=?", String.valueOf(id));
    }

    public Cloth findCloth(String name) {
        if (TextUtils.isEmpty(name)) return null;
        return LitePal.where("name=?", name).findFirst(Cloth.class);
    }

    public void order() {
//        List<cloth> books = LitePal.order("price desc").find(Book.class);
    }
}
